<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Http\Request;
use Request as Requests;

class AdminController extends Controller
{
    /**
     * @return string
     */
    public function admin()
    {
        return $admin = new Admin();
    }

    /**
     * 管理员注销
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getLogout()
    {
        /*用户的get方法请求登出视图*/
        $this->admin()->logout();
        return redirect('back/login');
    }

    /**
     * 管理员列表(查)
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getTable(Request $request){
        $search_url = substr(url(Requests::url()),22);
        $total = Admin::all();
        $data = Admin::where('name','like','%'.$request->input('keywords').'%')->paginate(3);
        return view('admin/admin_table',['data' => $data,'total' => $total,'search_url' => $search_url,'request' => $request->all()]);
    }

    /**
     * 添加管理员后的逻辑处理(增)
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function postTable(Request $request)
    {
        $result = $this->admin()->signup($request);
        if ($result['status'] == 0 )
            return back()->with('error', $result['error']);
        elseif ($result['status'] == 1 )
            return back()->with('msg', $result['msg']);
        elseif($result['status'] == -1 )
            return back()->withErrors($result['error']);
    }

    /**
     * 修改信息后的逻辑处理(改)
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function putEdit(Request $request)
    {
        $result = $this->admin()->edit($request);
        if ($result['status'] == -1 )
            return back()->withErrors($result['error']);
        elseif ($result['status'] == 0 )
            return back()->with('error', $result['msg']);
        elseif ($result['status'] == 1 )
            return back()->with('msg', $result['msg']);
    }

    /**
     *软删除(回收站操作)(删)
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete($id)
    {
        $result = $this->admin()->adminDelete($id);
        if($result['status'])
            return back()->with('msg',$result['msg']);
        else
            return back()->with('error',$result['msg']);
    }
}
